/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.framework.util;

import java.util.Comparator;


/**
 * List sorter that has a reverse flag.
 *
 * @author Tim wang
 * @version 1.0
 */
public class ColumnSorter implements ListSorter {
    static final long serialVersionUID = 123123L;
    transient boolean reverse = false;
    transient String property;

    /**
     * Constructs of the sorter.
     * @param property
     */
    public ColumnSorter(String property) {
        this.property = property;
    }

    /**
     * Sets reverse sorting.
     *
     * @param value flag value.
     */
    public void setRevers(boolean value) {
        reverse = value;
    }

    /**
     * Implementation of interface Comparator.
     * @see Comparator
     * for more details.
     */
    public int compare(Object o1, Object o2) {
        if ((o1 == null) || (o2 == null)) {
            return 0;
        }

        if (o1 instanceof Comparable && o2 instanceof Comparable) {
            int result = ((Comparable)o1).compareTo(o2);
            if (reverse) {
                return -result;
            } else {
                return result;
            }
        } else
            return 0;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getProperty() {
        return property;
    }

    public void setReverse(boolean reverse) {
        this.reverse = reverse;
    }

    public boolean isReverse() {
        return reverse;
    }
}
